<?php
require_once('./includes/headers.inc.php');

$error = FALSE;
if($_POST) {
    $ret = $userMgr->register($_POST);
    if($ret !== FALSE) {
        $error = $ret;
        $data = $_POST;
    } else {
        $userMgr->login($_POST['email'], $_POST['password']);
        $_SESSION['msg'] = $msgMgr->get('register-success');
        header('Location: /');
        exit();
    }
}

$languages = $langMgr->getLanguages();
$language_options = array();
foreach($languages as $row) {
    $language_options[] = '<option value="' . $row['id'] . '" ' . ($_POST && $_POST['language_id'] == $row['id'] ? 'selected' : '') . '>' . $row['name'] . '</a>';
}

$replaces = array(
    'email'             => (isset($data['email']) ? $data['email'] : ''),
    'name'              => (isset($data['name']) ? $data['name'] : ''),
    'language_options'  => implode("\n", $language_options),
    'error'             => $msgMgr->formatMsg($error),
);
$lang = array(
    'text_email'    => $langMgr->get('text_email'),
    'text_name'     => $langMgr->get('text_name'),
    'text_password' => $langMgr->get('text_password'),
    'text_language' => $langMgr->get('text_language'),
    'btn_register'  => $langMgr->get('btn_register'),
);
$templateMgr->loadContent(basename(__FILE__, '.php'), array_merge($replaces, $lang));
?>
